<template>
  <div class="login-wrapper">
    <div class="login-form-box">
      <h6>登录</h6>
      <var-form
        ref="loginRuleFormRef"
        :disabled="disabled"
        :readonly="readonly"
        scroll-to-error="start"
      >
        <var-space direction="column" :size="[14, 0]">
          <var-input
            clearable
            size="small"
            variant="outlined"
            placeholder="请输入用户名"
            :rules="[(v) => !!v || '用户名不能为空']"
            v-model="loginRuleForm.userName"
          >
            <template #prepend-icon>
              <var-icon name="account-circle" :size="18" />
            </template>
          </var-input>
          <var-input
            clearable
            size="small"
            variant="outlined"
            :type="mobilePassShowRef ? '' : 'password'"
            placeholder="请输入密码"
            :rules="[(v) => !!v || '密码不能为空']"
            v-model="loginRuleForm.passWord"
          >
            <template #prepend-icon>
              <var-icon name="pin" :size="18" />
            </template>
            <template #append-icon>
              <svg-icon
                :className="`svg-icon passShow`"
                :iconName="
                  mobilePassShowRef ? 'icon-password-show' : 'icon-xianshi'
                "
                :color="'#888'"
                @click="handleShowPass"
              ></svg-icon>
            </template>
          </var-input>
          <div class="captcha">
            <var-input
              clearable
              size="small"
              variant="outlined"
              placeholder="请输入验证码"
              :rules="[(v) => !!v || '验证码']"
              v-model="loginRuleForm.captcha"
            >
              <template #prepend-icon>
                <var-icon name="image" :size="18" />
              </template>
            </var-input>
            <img
              :src="captchaReadonly"
              height="30"
              v-if="captchaReadonly"
              class="captcha-img"
              @click="() => getImageCaptcha()"
            />
          </div>
        </var-space>
      </var-form>
      <var-button
        block
        class="save-btn"
        :loading="loginSaveLoadingReadonly"
        @click="handleLogin(loginRuleFormRef)"
      >
        登录
      </var-button>
      <div class="remember-register">
        <var-checkbox v-model="loginRuleForm.rememberPass"
          >记住密码</var-checkbox
        >
        <span @click="router.replace('/register')">注册</span>
      </div>
    </div>
  </div>
  <var-snackbar
    :type="loginAlterReadonly.type"
    v-model:show="loginAlterReadonly.show"
    >{{ loginAlterReadonly.title }}</var-snackbar
  >
</template>
<script setup name="Login">
import { inject, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import useSymbols from '../../compostions/useSymbols';

const router = useRouter();

const {
  mobilePassShowRef,
  loginRuleFormRef,
  loginRuleForm,
  loginSaveLoadingReadonly,
  loginAlterReadonly,
  captchaReadonly,
  getImageCaptcha,
  loginSubmitForm,
} = inject(useSymbols);

onMounted(() => {
  getImageCaptcha();
});

const handleLogin = (FormRef) => {
  loginSubmitForm(FormRef, goHome, true);
};

const goHome = () => {
  loginRuleFormRef.value.reset();
  mobilePassShowRef.value = false;
  router.replace({ path: '/' });
};

const handleShowPass = () => {
  mobilePassShowRef.value = !mobilePassShowRef.value;
};
</script>
<style scoped lang="less">
.login-wrapper {
  width: 100vw;
  height: 0;
  padding-bottom: 100vh;
  background-image: url('../../assets/imgs/mobile/loginBg.png');
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center center;
  overflow: hidden;
  position: relative;
  .login-form-box {
    position: absolute;
    top: calc(50% - 30vh);
    left: calc(50% - 47.5%);
    width: 95%;
    min-height: 26rem;
    max-height: 30rem;
    border-radius: 1.0667rem;
    background-color: rgba(255, 255, 255, 0.5);
    backdrop-filter: blur(0.8rem);
    box-sizing: border-box;
    padding: 1rem;
    h6 {
      font-size: 1.5rem;
      color: #444;
      margin: 2.6rem 0 2.5rem 0;
    }
    .captcha {
      display: grid;
      grid-template-columns: 1fr 6.9rem;
      grid-column-gap: 0.4rem;
      .captcha-img {
        border: 1px solid var(--field-decorator-blur-color);
        height: 2.4rem;
        border-radius: var(--field-decorator-line-border-radius);
      }
    }
    .passShow {
      width: 1.2rem;
      height: 1.1rem;
      margin-left: 1rem;
      cursor: pointer;
      &:hover {
        transform: scale(1.05);
      }
    }
    .save-btn {
      background-color: var(--system-primary-color);
      color: #fff;
    }
    /deep/ .var-space--auto {
      padding-bottom: 0.3rem;
    }

    /deep/ .var-field-decorator--focus {
      color: var(--system-primary-color);
    }
    /deep/ .var-field-decorator--outlined .var-field-decorator--line-focus {
      border-color: var(--system-primary-color);
    }
    /deep/ .var-field-decorator__middle {
      margin-left: 0.5rem;
    }
    .save-btn {
      margin-top: 2rem;
    }
    .remember-register {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: .8rem;
      padding: 0 .2rem;
      span{
        color:#397BFD ;
        font-size: .9rem;
      }
      /deep/ .var-checkbox--checked{
        color: var(--system-primary-color);
      }
    }
  }
}
</style>
